package com.glsc.ngateway.opmanage.controller.other;


import com.glsc.ngateway.common.api.common.ex.Result;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.dto.other.TradeSeatFeesDto;
import com.glsc.ngateway.common.api.platform.dto.other.TradeSeatFlowSpeedImportDto;
import com.glsc.ngateway.common.api.platform.dto.other.TradeSeatSpeedFlowDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.TradeSeatFeesItem;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.TradeSeatFeesLog;
import com.glsc.ngateway.opmanage.dto.OutSystemExcelDto;
import com.glsc.ngateway.opmanage.service.other.TradeSeatFeesService;
import com.glsc.ngateway.opmanage.utils.report.ReportUploadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;


/**
 * @Author: liwei
 * @Desc: 交易单元费用
 */
@RestController
@RequestMapping("/api/tradeSeatFees")
@Api(value = "TradeSeatRentController", tags = {"交易单元信息管理"})
public class TradeSeatFeesController {

    private static Logger logger = LoggerFactory.getLogger(TradeSeatFeesController.class);

    @Resource
    private TradeSeatFeesService tradeSeatFeesService;


    @GetMapping("/listpage")
    @ApiOperation(value = "交易单元费用预估分页查询", notes = "交易单元费用预估分页查询")
    public Result<Page<TradeSeatFeesDto>> listPage(@ApiParam(value = "市场") @RequestParam(required = false) String tradeSeatMarket,
                                                   @ApiParam(value = "交易单元号") @RequestParam(required = false) String tradeSeatCode,
                                                   @ApiParam(value = "起始日期") @RequestParam(required = false) String beginDate,
                                                   @ApiParam(value = "截止日期") @RequestParam(required = false) String endDate,
                                                   @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                                   @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {
        Page<TradeSeatFeesDto> page = tradeSeatFeesService.findPage(tradeSeatMarket, tradeSeatCode, beginDate, endDate, PageUtil.initPageNo(pageNo), PageUtil.initPageSize(pageSize));
        return Result.successData(page);
    }

    @GetMapping("/item/listpage")
    @ApiOperation(value = "交易单元费用预估Item分页查询", notes = "交易单元费用预估Item分页查询")
    public Result<Page<TradeSeatFeesItem>> listItemPage(@ApiParam(value = "交易单元号") @RequestParam(required = false) String tradeSeatCode,
                                                        @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                                        @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {
        Page<TradeSeatFeesItem> page = tradeSeatFeesService.findItemPage(tradeSeatCode, PageUtil.initPageNo(pageNo), PageUtil.initPageSize(pageSize));
        return Result.successData(page);
    }

    @GetMapping("/flowSpeed/listpage")
    @ApiOperation(value = "流速权分页查询", notes = "流速权分页查询")
    public Result<Page<TradeSeatSpeedFlowDto>> listFlowSpeedPage(@ApiParam(value = "交易单元号") @RequestParam(required = false) String tradeSeatCode,
                                                                 @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                                                 @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {
        Page<TradeSeatSpeedFlowDto> page = tradeSeatFeesService.findFlowSpeedPage(tradeSeatCode, PageUtil.initPageNo(pageNo), PageUtil.initPageSize(pageSize));
        return Result.successData(page);
    }


    @GetMapping("/log/listpage")
    @ApiOperation(value = "交易单元费用预估Item分页查询", notes = "交易单元费用预估Item分页查询")
    public Result<Page<TradeSeatFeesLog>> listLogPage(@ApiParam(value = "交易单元号") @RequestParam(required = false) String tradeSeatCode,
                                                      @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                                      @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {
        Page<TradeSeatFeesLog> page = tradeSeatFeesService.findLogPage(tradeSeatCode, PageUtil.initPageNo(pageNo), PageUtil.initPageSize(pageSize));
        return Result.successData(page);
    }


    @PostMapping("/flowSpeed/upload")
    @ApiOperation(value = "流速权导入", notes = "导入编辑")
    public PlatformResponse<?> upload(@RequestPart("file") MultipartFile file) throws IOException {
        ReportUploadContext<TradeSeatFlowSpeedImportDto, String> context = tradeSeatFeesService.upload(file);
        return context.isCheckPass() ? PlatformResponse.success() : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @GetMapping("/am/email")
    @ApiOperation(value = "发送资管子交易单元预估费用信息", notes = "发送资管子交易单元预估费用信息")
    public PlatformResponse<?> listLogPage() {
        tradeSeatFeesService.amFeeForecastEmail();
        return PlatformResponse.success();
    }

    @PostMapping("/importSh")
    @ApiOperation(value = "读取上海交易单元excel信息", notes = "读取上海交易单元excel信息")
    public PlatformResponse<?> readShTreadSeatInfo(@RequestPart("file") MultipartFile file) throws IOException {
        logger.info("读取上海交易单元excel信息");
        tradeSeatFeesService.readShTreadSeatInfo(file);
        return PlatformResponse.success();
    }

    @PostMapping("/importSz")
    @ApiOperation(value = "读取深圳交易单元excel信息", notes = "读取深圳交易单元excel信息")
    public PlatformResponse<?> readSzTreadSeatInfo(@RequestPart("file") MultipartFile file) throws IOException {
        logger.info("读取深圳交易单元excel信息");
        tradeSeatFeesService.readSzTreadSeatInfo(file);
        return PlatformResponse.success();
    }

}
